package com.famdoc.Service.Impl;

import com.famdoc.Mapper.IndexMapper;
import com.famdoc.Pojo.dto.Result;
import com.famdoc.Pojo.entity.Patient;
import com.famdoc.Pojo.params.IndexBodyParams;
import com.famdoc.Pojo.vo.IndexBodyVO;
import com.famdoc.Pojo.vo.IndexHeaderVO;
import com.famdoc.Service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexMapper indexMapper;

    /**
     * 获取首页头部数据
     * @param doctorId
     * @return
     */
    @Override
    public Result getIndexHeaderData(Integer doctorId) {
        //获取今天开始的时间和结束的时间
        LocalDateTime startOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        //获取现在的时间、12小时后的时间和一周后的时间
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime nearlyDueTime = LocalDateTime.now().plusHours(12);
        LocalDateTime nearlyWeekDueTime = LocalDateTime.now().plusWeeks(1);
        //获取今天增加的签约数
        int todayAddSign = indexMapper.getTodayAddSign(doctorId,startOfDay,endOfDay);
        //获取已经随访总人数
        int totalVisited = indexMapper.getTotalVisited(doctorId);
        //获取累计签约数
        int totalSign = indexMapper.getTotalSign(doctorId);
        //获取已逾期的签约总数
        int totalDue = indexMapper.getTotalDue(doctorId,now);
        //获取即将逾期的签约总数
        int nearlyDue = indexMapper.getNearlyDue(doctorId,now,nearlyDueTime);
        //获取将于一周内逾期签约总数
        int nearlyWeekDue = indexMapper.getNearlyWeekDue(doctorId,now,nearlyWeekDueTime);
        IndexHeaderVO indexHeaderVO = new IndexHeaderVO(todayAddSign,totalVisited,totalSign,
                nearlyDue,nearlyWeekDue,totalDue);
        return Result.success(indexHeaderVO);
    }

    /**
     * 获取首页工作提醒数据
     * @param doctorId
     * @return
     */
    @Override
    public Result getIndexBodyData(Integer doctorId) {
        //获取人群分类名列表
        List<String> crowdNameList = indexMapper.getCrowdNames();
        List<IndexBodyVO> indexBodyVOList = new ArrayList<>();
        IndexBodyParams indexBodyParams = new IndexBodyParams(doctorId);
        System.out.println(indexBodyParams);
        int i = 0;
        for (String crowdName : crowdNameList) {
            if(i == 0 || i == 1){
                i++;
                continue;
            }
            indexBodyParams.setCrowdName(crowdName);
            IndexBodyVO indexBodyVO = new IndexBodyVO();
            //设置人群分类名
            indexBodyVO.setCrowdName(crowdName);
            //分类获取首次随访未进行数
            indexBodyVO.setFirstVisit(indexMapper.getFirstVisit(indexBodyParams));
            //分类获取本日应随访数
            indexBodyVO.setTodayVisit(indexMapper.getTodayVisit(indexBodyParams));
            //分类获取本周应随访数
            indexBodyVO.setWeekVisit(indexMapper.getWeekVisit(indexBodyParams));
            //分类获取已逾期随访数
            indexBodyVO.setDueVisit(indexMapper.getDueVisit(indexBodyParams));
            indexBodyVOList.add(indexBodyVO);
        }
        return Result.success(indexBodyVOList);
    }

    /**
     * 获取所有签约即将到期的患者
     * @param keyword
     * @return
     */
    @Override
    public Result getAllNearlyDue(Integer doctorId,String keyword) {
        //获取现在的时间、12小时后的时间和一周后的时间
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime nearlyWeekDueTime = LocalDateTime.now().plusWeeks(1);
        //通过关键字获取所有的签约即将到期的病人
        List<Patient> patientList = indexMapper.getAllNearlyDueByKeyWord(doctorId,keyword,now,nearlyWeekDueTime);
        return Result.success(patientList.stream().distinct());
    }

    /**
     * 获取所有签约到期的患者
     * @param doctorId
     * @param keyword
     * @return
     */
    @Override
    public Result getAllDue(Integer doctorId, String keyword) {
        //通过关键字获取所有的签约到期的病人
        LocalDateTime now = LocalDateTime.now();
        List<Patient> duePatientList = indexMapper.getAllDueByKeyWord(doctorId,keyword,now);
        return Result.success(duePatientList.stream().distinct());
    }

    /**
     * 获取工作提醒上按人群分类名分类的病人列表
     *
     * @param doctorId
     * @param crowdName
     * @param type
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result getVisitPatientByCrowd(Integer doctorId, String crowdName,
                                         Integer type, Integer page, Integer pageSize) {
        List<Patient> patientList = new ArrayList<>();
        int start = (page-1)*pageSize;
        //new查询参数类
        IndexBodyParams indexBodyParams = new IndexBodyParams(doctorId);
        indexBodyParams.setCrowdName(crowdName);
        indexBodyParams.setStart(start);
        indexBodyParams.setPageSize(pageSize);
        if(type==1){
            //按人群分类获取随访未进行患者列表
            patientList = indexMapper.getFirstVisitPatientByCrowd(indexBodyParams);
        }else if(type==2){
            //按人群分类获取今日应随访患者列表
            patientList = indexMapper.getTodayVisitPatientByCrowd(indexBodyParams);
        }else if(type==3){
            //按人群分类获取本周应随访患者列表
            patientList = indexMapper.getWeekVisitPatientByCrowd(indexBodyParams);
        }else if(type==4){
            //按人群分类获取已逾期随访患者列表
            patientList = indexMapper.getDueVisitPatientByCrowd(indexBodyParams);
        }
        return Result.success(patientList.stream().distinct());
    }
}
